<html>
<head><meta charset="utf-8"><title>Shortening inline hints · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html">Shortening inline hints</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="177809621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177809621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177809621">(Oct 10 2019 at 13:30)</a>:</h4>
<p>For shortening inline type hints, I'd hoped to transform the type a bit to change long parts to <code>_</code> or <code>...</code> or similar.<br>
Is it better to replace those parts with some dummy types that print the right thing, or is it possible to display just parts of the type without re-implementing the whole formatting part?</p>



<a name="177809819"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177809819" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177809819">(Oct 10 2019 at 13:32)</a>:</h4>
<p>I guess the formatting part could be refactored to take an optional depth parameter? Maybe using unlimited depth for when you hover over the declaration, but inline hints and hovering over uses could be limited too</p>



<a name="177809871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177809871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177809871">(Oct 10 2019 at 13:33)</a>:</h4>
<p>Good question! I don't know the right answer :)</p>
<p>I think we need some of general formatting infra with knobs in ra_hir, and use it for both HirDispaly and type hints. Basically, what <span class="user-mention" data-user-id="200146">@Daniel Mcnab</span> suggesting</p>



<a name="177810052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177810052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177810052">(Oct 10 2019 at 13:35)</a>:</h4>
<p>Type hints use the HirDisplay trait, so it makes sense to extend that then, yeah</p>



<a name="177810191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177810191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177810191">(Oct 10 2019 at 13:36)</a>:</h4>
<p><span class="user-mention" data-user-id="125131">@detrumi</span> I had something different in mind. I think extending <code>HirDisplay</code> would put complexity in the wrong place.</p>
<p>Rather, we should extend only <code>Ty</code> display, but implmenet <code>&lt;Ty as HirDisplay&gt;</code> in terms of that new infra</p>



<a name="177810233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177810233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177810233">(Oct 10 2019 at 13:37)</a>:</h4>
<p>basically</p>
<div class="codehilite"><pre><span></span>fn display_ty(ty: &amp;Ty, knobs: Kbobs) -&gt; Stirng { ... }

impl HirDispaly for Ty {
    fn fmt(&amp;self) {  display_ty(self, Knobs::default()) }
}
</pre></div>



<a name="177810267"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177810267" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177810267">(Oct 10 2019 at 13:37)</a>:</h4>
<p>Oooh, that's nice</p>



<a name="177811781"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177811781" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177811781">(Oct 10 2019 at 13:54)</a>:</h4>
<p>Well... extending only <code>Ty</code> display would mean the knobs would get lost e.g. in <code>impl Trait&lt;SomeType&gt;</code></p>



<a name="177811849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177811849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177811849">(Oct 10 2019 at 13:55)</a>:</h4>
<p>=/</p>



<a name="177811857"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177811857" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177811857">(Oct 10 2019 at 13:55)</a>:</h4>
<p>unless you mean completely building a parallel displaying infrastructure that allows the knobs</p>



<a name="177811880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177811880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177811880">(Oct 10 2019 at 13:55)</a>:</h4>
<p>then we could just implement <code>HirDisplay</code> for that</p>



<a name="177811942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177811942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177811942">(Oct 10 2019 at 13:56)</a>:</h4>
<p>I think we need something like that</p>



<a name="177812860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177812860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177812860">(Oct 10 2019 at 14:05)</a>:</h4>
<p>Might be a good idea to check what itellij is doing</p>



<a name="177814218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177814218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177814218">(Oct 10 2019 at 14:19)</a>:</h4>
<p>Looks they're building a collapsible structure based on the depth</p>



<a name="177814340"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177814340" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177814340">(Oct 10 2019 at 14:20)</a>:</h4>
<p>Or levels, with some logic for checking sizes</p>



<a name="177814369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177814369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177814369">(Oct 10 2019 at 14:20)</a>:</h4>
<p>Oooh, right, the don't render directly to string....</p>
<p>I wonder if we should actually do the same? Produce a tree, with a hints like "you can collapse this node", and let the client handle getting the actual string</p>



<a name="177814684"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177814684" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177814684">(Oct 10 2019 at 14:24)</a>:</h4>
<p>We could, though it would complicate the client somewhat</p>



<a name="177814760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177814760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177814760">(Oct 10 2019 at 14:25)</a>:</h4>
<p>Unless we let clients choose between string or tree representation</p>



<a name="177814795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177814795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177814795">(Oct 10 2019 at 14:25)</a>:</h4>
<p>That's true.... Yeah, I think, as a first cut, we really should just be sending strings to the editor. But it makes sense to keep a more powerful impl in mind while implementhig this</p>



<a name="177824805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177824805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177824805">(Oct 10 2019 at 16:02)</a>:</h4>
<p>Yeah, I think building some tree structure and handling the presentation/collapsing stuff etc. as a second step is actually a good approach</p>



<a name="177824836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177824836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177824836">(Oct 10 2019 at 16:02)</a>:</h4>
<p><span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span> maybe that tree structure is just a syntax tree again?</p>



<a name="177908072"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/177908072" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#177908072">(Oct 11 2019 at 13:48)</a>:</h4>
<p>Ideally we'd create some indirection in the <code>hir_fmt</code> functions, so that they don't call <code>hir_fmt</code> on children directly. Maybe change <code>ty.hir_fmt(f)</code> to something like <code>f.fmt(ty)</code>, where a different formatter can handle the level logic?</p>



<a name="181014510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/181014510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#181014510">(Nov 18 2019 at 13:20)</a>:</h4>
<p>So I've been experimenting a bit on this, and there's a few ways to go here:<br>
1) The simplest is to still write directly to string, but add some knobs (just a folding level in the simplest case). This is fairly easy to add to <code>HirDisplayWrapper</code>, <a href="https://github.com/rust-analyzer/rust-analyzer/compare/master...detrumi:hir-display-levels" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/compare/master...detrumi:hir-display-levels">this WIP branch</a> shows an example<br>
2A) Changing <code>HirDisplay</code> so that it returns an intermediate tree representation that tracks which parts can be collapsed, so we can use a single <code>HirDisplay</code> implementation for both type hints and normal <code>Display</code> impls. I've got a <a href="https://github.com/rust-analyzer/rust-analyzer/compare/master...detrumi:hir-display-tree" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/compare/master...detrumi:hir-display-tree">very WIP branch</a> that shows the idea. The main downside of this approach is that the <code>HirDisplay</code> impl's get more complex because they need to decide what's collapsible and such<br>
2B) Create new infrastructure for showing type hints that's separate from <code>HirDisplay</code>. This is <a href="https://github.com/intellij-rust/intellij-rust/blob/f884688720a04cb69d41b48acfe6318a24a8ccba/src/main/kotlin/org/rust/ide/hints/RsTypeHintsPresentationFactory.kt#L22" target="_blank" title="https://github.com/intellij-rust/intellij-rust/blob/f884688720a04cb69d41b48acfe6318a24a8ccba/src/main/kotlin/org/rust/ide/hints/RsTypeHintsPresentationFactory.kt#L22">what intellij is doing</a>. The main downside of course is that we duplicate the formatting implementations, and we won't be able to send the tree representation to the client</p>



<a name="181095995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Shortening%20inline%20hints/near/181095995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Shortening.20inline.20hints.html#181095995">(Nov 19 2019 at 09:51)</a>:</h4>
<p>Ah, seems like kiljacken had more success implementing a solution, nice</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>